import 'package:example/common/langs/index.dart';
import 'package:example/common/model/base_model.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold/common/http/http_manager.dart';
import 'package:getx_scaffold/common/http/result_analyzed.dart';
import 'package:getx_scaffold/getx_scaffold.dart';

import 'index.dart';

class NetworkPage extends GetView<NetworkController> {
  const NetworkPage({super.key});

  // 主视图
  Widget _buildView(context) {
    return <Widget>[
      ListTile(
        title: const Text('设置BaseURL'),
        subtitle: const Text('HttpManager.baseUrl = "https://api.vvhan.com"'),
        onTap: () => HttpManager.baseUrl = 'https://api.azr.top',
      ),
      ListTile(
        title: const Text('设置Headers11'),
        subtitle: Text('HttpManager.setHeaders({"key":"value"});', style: TextStyle(color: Theme.of(context).colorScheme.secondary),),
        onTap: () {
          final headers = {
            "UserToken": "12309123aes2233sd35435sxc23234",
            "DeviceModel": "iPhone"
          };
          HttpManager.setHeaders(headers);
        },
      ),
      ListTile(
        title: const Text('清除Headers'),
        subtitle: const Text("HttpManager.clearHeaders();"),
        onTap: () => HttpManager.clearHeaders(),
      ),
      ListTile(
        title: const Text('设置Authorization'),
        onTap: () {
          HttpManager.setAuthorization('1234567890');
        },
        subtitle: const Text("HttpManager.authorization = '1234567890';"),
      ),
      ListTile(
        title: const Text('清除Authorization'),
        subtitle: const Text("HttpManager.clearAuthorization();"),
        onTap: () {
          HttpManager.clearAuthorization();
        },
      ),
      ListTile(
        title: const Text('设置响应数据的统一解析处理'),
        onTap: () {
          //  解析服务器返回的 json 字典
          //  假如服务器返回：{"code":200, "message": "请求成功", "data": [{},{}]}
          //  那么应该设置为 ResponseAnalyzed.setResponseKey("code", "message", "data");
          //
          //  如果服务器返回：{"code":200, "msg": "请求成功", "response": [{},{}]}
          ResponseAnalyzed.setResponseKey("code", "message", "data");
        },
        subtitle: const Text('ResponseAnalyzed.setResponseKey("code", "message", "data");'),
      ),
      ListTile(
        title: const Text('解析响应数据'),
        onTap: () {
          // HttpManager.setOnResponseHandler(null);
        },
        subtitle: const Text("HttpManager.getAnalyzed(...);\nHttpManager.postAnalyzed(...);"),
      ),
      ListTile(
        title: const Text('不解析响应数据'),
        onTap: () {
          // HttpManager.setOnResponseHandler(null);
        },
        subtitle: const Text("HttpManager.get(...);\nHttpManager.post(...);"),
      ),
      ListTile(
        title: const Text('设置响应数据的统一拦截处理'),
        onTap: () {
          ResponseAnalyzed.setResultCheck((result) {
            var code = int.parse(result.code??"0");
            if (code == 20401) {
              // 退出登录
            }else if (code > 20000 && code < 30000) {
              debugPrint('code: $code, message: ${result.message!}');
              result.isExposedToUser = true;
              showMessageToast(result.message!);
              debugPrint("result.message! = ${result.message!}");
            }
          });
        },
      ),
      ListTile(
        title: const Text('get请求接口'),
        onTap: () async {
          showLoadingMessage("正在加载");
          var response = await HttpManager.get('/api/wallpaper/acg?type=json');
          dismissLoading();
          var result = BaseModel.fromJson(response.data);
          showMessageToast(result.url ?? '');
        },
      ),
      ListTile(
        title: const Text('取消网络请求'),
        onTap: () async {
          Loading.showLoading("操作成功");
          controller.cancelToken = CancelToken();
          HttpManager.get('/api/wallpaper/acg?type=json', cancelToken: controller.cancelToken)
              .then((value) {
            Loading.dismissLoading();
          });
          delayed(200, () => HttpManager.cancel(controller.cancelToken));
        },
      ),
      ListTile(
        title: const Text('取消全部网络请求'),
        onTap: () async {
          HttpManager.cancel();
        },
      ),
    ].toListView(
      separator: const DividerX(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<NetworkController>(
      init: NetworkController(),
      id: 'network',
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text(TextKey.wangLuoQingQiu.tr), elevation: 1),
          body: SafeArea(
            child: _buildView(context),
          ),
        );
      },
    );
  }
}
